package com.example.yunxiappbackend.config;

import com.example.yunxiappbackend.controller.interceptor.BackgroundLoginInterceptor;
import com.example.yunxiappbackend.controller.interceptor.FrontLoginInterceptor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.converter.HttpMessageConverter;
import org.springframework.http.converter.json.MappingJackson2HttpMessageConverter;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurationSupport;
import java.util.List;

@Configuration
public class WebConfig extends WebMvcConfigurationSupport {
    @Autowired
    FrontLoginInterceptor loginInterceptor;
    @Autowired
    BackgroundLoginInterceptor backgroundLoginInterceptor;

    @Override
    protected void addInterceptors(InterceptorRegistry registry) {
        registry.addInterceptor(loginInterceptor)
                .addPathPatterns("/mainPage/**","/user/**");
        registry.addInterceptor(backgroundLoginInterceptor)
                .addPathPatterns("/backgroundData/**");
    }

    @Override
    protected void addResourceHandlers(ResourceHandlerRegistry registry) {
        registry.addResourceHandler("/**")
                .addResourceLocations("classpath:/static/");
    }
    @Override
    protected void extendMessageConverters(List<HttpMessageConverter<?>> converters) {
        //创建消息转换器对象
        MappingJackson2HttpMessageConverter jackson2HttpMessageConverter = new MappingJackson2HttpMessageConverter();
        //设置JSON转换器，底层使用Jackson转换
        jackson2HttpMessageConverter.setObjectMapper(new JacksonObjectMapper());
        //将上面对象添加到MVC转换器集合中
        converters.add(0,jackson2HttpMessageConverter);
    }
}
